<%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<%@ include file="/static/include/taglib.jsp" %>
<html>
<head>
    <title>Title</title>
    <link href="${ctxStatic}/layui/css/layui.css" rel="stylesheet"/>
    <link href="${ctxStatic}/css/kpiui.css" rel="stylesheet"/>
    <script src="${ctxStatic}/js/jquery-1.8.3.min.js?v=2.0" type="text/javascript"></script>
    <script src="${ctxStatic}/layui/layui.all.js" type="text/javascript"></script>
    <script src="${ctxStatic}/js/common.js" type="text/javascript"></script>

    <style>

        .text-center {

            text-align: center;
        }

        .layui-table-cell {
            /*height: 28px;*/ /*注释掉*/
            height: auto; /*新添加的*/
            line-height: 28px;
            /* padding: 0 15px; */
            position: relative;
            overflow: hidden;
            /* text-overflow: ellipsis; */
            white-space: normal;
            box-sizing: border-box;
        }

        .tableTitle {

            text-align: center;
        }
    </style>
</head>
<body>


<div class="serchcontent" style="margin: 20px 20px">
    <div class="search">
        <div class="layui-form">
            <table nostyle="1">
                <tr>
                    <td class="td_select">
                        <select id="year" placeholder="年份" lay-filter="year">
                            <option value="2018">2018年</option>
                            <option value="2017">2017年</option>
                        </select>
                        <input type="hidden" id="hidyear" value="${year }">
                    </td>
                    <td class="td_select">
                        <select id="month" lay-filter="month">
                            <option value="1">1季度</option>
                            <option value="2">2季度</option>
                            <option value="3">3季度</option>
                            <option value="4">4季度</option>

                        </select>
                        <input type="hidden" id="hidmonth" value="${month}">
                    </td>

                    <td>

                        <select id="szdw" name="szdw" lay-filter="szdw">
                            <c:forEach var="item" items="${orglst}" varStatus="status">
                                <c:if test="${status.index==0}">

                                    <option value="${item.orgid}">${item.orgname}</option>


                                </c:if>

                                <c:if test="${status.index!=0}">

                                    <option value="${item.orgid}">${item.orgname}</option>

                                </c:if>

                            </c:forEach>
                        </select>
                        <input type="hidden" id="hiddenValue" value="${defaultValue}">
                    </td>
                    <td style="text-align: center;width:300px">
                        <button class="button" onclick="search()"><img
                                src="${ctxStatic}/images/button/chaxun.png" style="width:20px">查询
                        </button>
                        <button class="button" onclick="save()"> 保存
                        </button>

                        <button class="button" onclick="excle()"> 导出
                        </button>
                    </td>
                </tr>
            </table>
        </div>
    </div>

    <div>

        <div class="tableTitle">
            <h3><span class="titleDW"></span> 单位 <span class="titleYear"></span>年第 <span class="titleQuarter"></span>季度参加评议不定等次（经批准不参加评议）
            </h3>
            <h3>党员汇总表</h3>


        </div>
        <div id="list" lay-filter='test' class="layui-table" lay-data="{height: 'full-200', cellMinWidth: 80}"></div>
    </div>

</div>
</div>
</body>
<script>
    var year = new Date().getFullYear();
    var quarter = getQuarter();
    var _table;

    $(function () {

        if (quarter == 1) {
            year = new Date().getFullYear() - 1;
            $('#year').val(year);
            quarter = 4;


        }
        var form;
        $('#month').val(quarter);
        layui.use('form', function () {
            form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
            form.render();
        });
        var options = $('#szdw option:selected'); //获取选中的项
        setTitle(options.text(), year, quarter)
        form.on('select(year)', function (data) {

            year = data.value;
            var options = $('#szdw option:selected'); //获取选中的项


            setTitle(options.text(), data.value, quarter)

        });
        form.on('select(month)', function (data) {
            var options = $('#szdw option:selected'); //获取选中的项
            quarter = data.value;
            setTitle(options.text(), year, quarter)

        })
        form.on('select(szdw)', function (data) {
            var options = $('#szdw option:selected'); //获取选中的项
            setTitle(options.text(), year, quarter)


        })

        load();
    })

    function setTitle(dw, year, quarter) {
        $('.titleYear').text(year);
        $('.titleDW').text(dw);
        $('.titleQuarter').text(quarter);

    }


    function getQuarter() {

        var month = new Date().getMonth() + 1;

        if (month <= 3) {

            return 1;

        } else if (month <= 6) {

            return 2-1;

        } else if (month <= 9) {

            return 3-1;

        } else if (month <= 12) {

            return 4-1;

        }

    };


    function getDw() {


        if ($('#szdw').val() == $('#hiddenValue').val()) return 0;
        return $('#szdw').val();
    }

    function load() {
        var index = layer.load();
        layui.use('table', function () {
            table = layui.table;
            table.render({
                elem: '#list'
                , height: $(window).height() - 100
                , url: 'getDY' // 数据接口

                , id: 'templatelist'
                , method: "get"
                , cols: [[ // 表头
                    {type: 'numbers'}
                    , {field: 'orgname', title: '单位', align: 'center', width: 220}
                    , {field: 'realname', title: '姓名', align: 'center', width: 100}
                    , {field: 'age', title: '年龄', align: 'center'}
                    , {field: 'sexText', title: '性别', align: 'center'}
                    , {field: 'cardno', title: '身份证号', align: 'center', width: 180}
                    , {field: 'orgname', title: '所属单位(支部)', align: 'center', width: 220}
                    , {field: 'xrzw', title: '现任职务', align: 'center', width: 180}
                    , {
                        field: 'wgwjsm',
                        title: '参加不定等次(或批准不参加评议)原因',
                        edit: 'text',
                        align: 'center',
                        width: 280
                    }
                    , {field: 'remark', title: '备注', edit: 'text', align: 'center', width: 200}
                ]]
                , request: {
                    type: 'get'
                }
                , page: false
                , even: true //开启隔行背景
                //,size: 'sm' //小尺寸的表格
                , where: {year: year, quarter: quarter, dw: getDw()}
                , done: function (res, page, count) {
                    layer.close(index);
                    $("[data-field='xrzw']").children().each(function (index) {
                        if (index > 0) {
                            var text = $(this).text();
                            var html = '';
                            if (text != "" && text != null) {
                                text = parseInt(text)
                                switch (text) {

                                    case 1:
                                        html = '处级领导干部';
                                        break;
                                    case  2:
                                        html = '科级领导干部';
                                        break;
                                    case 3:
                                        html = '民警';
                                        break;
                                    case  4:
                                        html = '未定职务(新录用公务员)';
                                        break;
                                    case 5:
                                        html = '领导班子';
                                        break;

                                }

                                $(this).text(html);
                            }
                        }
                    })
                }

            });


        });

        table.on('edit(test)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            save(function () {
                top.mylayui.msg('已自动保存', {icon: 1});

            });
        });

        _table = table;
    }

    // Template.prototype.searchtable = function () {
    //
    // }


    function save(callback) {
        var data = _table.cache.templatelist;
        var list = [];
        for (var i = 0; i < data.length; i++) {
            var element = data[i];

            list.push({

                userid: element.userid,
                year: year,
                quarter: quarter,
                opinion: element.wgwjsm,
                remark: element.remark

            })
        }


        $.ajax({
            type: "POST",
            url: "saveDBDD",
            data: {
                json: JSON.stringify(list),
                year: year,
                quarter: quarter

            },
            beforeSend: function (XHR) {

            },
            success: function (data) {
                if (callback) callback();
                else top.mylayui.msg('保存成功', {icon: 1});

            },
            complete: function (XHR) {

            },
            error: function () {

            }
        });

    }

    function excle() {
        var tableJson = _table.cache;
        var year = $("#year").val();
        ;
        var quarter = $("#month").val();
        var orgname = $.trim($("#szdw").find("option:selected").text());
        tableJson['yearid'] = year;
        tableJson['quarter'] = quarter;
        tableJson['orgname'] = orgname;
        tableJson = JSON.stringify(tableJson);
        tableJson = tableJson.replace(/LAY_TABLE_INDEX/g, 'lay_TABLE_INDEX');
        var index = layer.load();
        $.ajax({
            type: "POST",
            url: "bddc_export",
            contentType: 'application/json',
            data: tableJson,
            beforeSend: function (XHR) {
            },
            success: function (data) {
                data = eval('(' + data + ')');
                if (data.result > 0) {

                    //top.mylayui.msg('导出成功', {icon: 1});
                    //document.location.reload();
                    window.open(data.msg, '_self');
                }
                else {
                    top.mylayui.msg(data.msg, {icon: 0});
                }
            },
            complete: function (XHR) {
                layer.close(index);
            },
            error: function () {
                layer.close(index);
            }
        });
    }


    function search() {


        _table.reload('templatelist', {

            page: false
            , where: {
                year: year,
                quarter: quarter,
                dw: getDw()
            }
        });


    }


</script>
</html>
